package com.totoro.rabbitmq.consumer;

import com.rabbitmq.client.Channel;
import com.totoro.rabbitmq.RabbitMQConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * RabbitMQ消息监听类
 * Created by Administrator on 2017/9/14.
 */
@Component
public class RabbitMQMsgListener {
    /**
     * 简单的消息监听容器
     * @return
     */
    @Bean
    public SimpleMessageListenerContainer simpleMessageListenerContainer(ConnectionFactory connectionFactory){
        SimpleMessageListenerContainer smlc = new SimpleMessageListenerContainer(connectionFactory) ;
        smlc.setQueueNames(RabbitMQConfig.QUEUE);//设置监听的队列

        smlc.setMessageListener(new ChannelAwareMessageListener() {
            @Override
            public void onMessage(Message message, Channel channel) throws Exception {
                byte[] body = message.getBody() ;
                System.out.println("hello_queue收到的参数为："+ new String(body));
            }
        });
        return smlc ;
    }

    @Bean
    public SimpleMessageListenerContainer simpleMessageListenerContainer_add(ConnectionFactory connectionFactory){
        SimpleMessageListenerContainer smlc = new SimpleMessageListenerContainer(connectionFactory) ;
        smlc.setQueueNames(RabbitMQConfig.QUEUE_add);

        smlc.setMessageListener(new ChannelAwareMessageListener() {
            @Override
            public void onMessage(Message message, Channel channel) throws Exception {
                byte[] body = message.getBody() ;
                System.out.println("add_queue收到的参数为："+ new String(body));
            }
        });
        return smlc ;
    }


}
